package com.mapforge.gui.tools;

import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;

import com.mapforge.Constants;
import com.mapforge.listener.TileMapCreationEvent;
import com.mapforge.listener.TileMapCreationListener;
import com.mapforge.map.TileMap;
import com.mapforge.map.tools.ZoomTool;
import com.mapforge.map.tools.undo.tilemapedit.ZoomEdit;

public class ZoomToolButton extends MapToolButton implements
		TileMapCreationListener, UndoableEditListener {
	/**
	 * Default serial version ID, here to remove warnings.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The tile map being edited.
	 */
	private TileMap tileMap;
	
	/**
	 * The panel providing additional controls for this zoom tool
	 */
	ZoomToolPanel zoomToolPanel;

	public ZoomToolButton(ZoomTool zoomTool) {
		super(Constants.ZOOM_IN_ICON, zoomTool, new ZoomToolPanel(zoomTool));
		zoomToolPanel = (ZoomToolPanel) getMapToolPanel();
		zoomTool.addUndoableEditListener(this);
	}

	@Override
	public void tileMapCreationPerformed(TileMapCreationEvent mapCreationEvent) {
		this.tileMap = mapCreationEvent.getTileMapCreated();
		this.getMapTool().setTileMap(this.tileMap);
		this.zoomToolPanel.setTileMap(this.tileMap);
	}

	@Override
	public void undoableEditHappened(UndoableEditEvent e) {
		if (e.getEdit() instanceof ZoomEdit) {
			zoomToolPanel.setTileMap(this.tileMap);
			zoomToolPanel.repaint();
		}
	}

}
